﻿Public Class frmMain
    Dim objDB As New clsHWSetting
    Dim a As Boolean = True
    Private th As System.Threading.Thread
    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        connectDB()
        th = New Threading.Thread(AddressOf doActive)
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub
    Public Sub connectDB()
        'イニシャルファイル情報取得
        g_szInitFileName = My.Application.Info.DirectoryPath & "\LHJServer.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        '得到当前用户名
        g_LoginUser = GetIniFile("LOGIN", "user", "admin", g_szInitFileName)

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If

        setAllErrMsg()
    End Sub

    Private Sub doActive()
        While True
            Dim allLHJ As DataTable = objDB.selectALL()
            For Each lhj As DataRow In allLHJ.Rows
                lblLHJ.Text = lhj("equip_id")
                'Application.DoEvents()
                If My.Computer.Network.Ping(lhj("ip")) = False Then
                    If lhj("status") <> 2 Then
                        objDB.setLHJStatus(lhj("equip_id"), 2)
                    End If

                End If
            Next

        End While
    End Sub

    Private Sub btnActive_Click(sender As Object, e As EventArgs) Handles btnActive.Click


        If btnActive.Text = "开始" Then
            th.Start()

            btnActive.Text = "结束"
        Else
            If th.IsAlive Then th.Suspend()
            btnActive.Text = "开始"
        End If
    End Sub

    Protected Overrides Sub Finalize()
        If th.IsAlive Then th.Suspend()
        MyBase.Finalize()
    End Sub
End Class

Public Class clsHWSetting

    Public Function selectALL() As DataTable
        Dim szSql As String = String.Empty
        szSql &= "SELECT * "
        szSql &= "  FROM sf.t_hw_setting "
        szSql &= "  WHERE factory_id =? "
        szSql &= "  AND workshop_id = ? "
        szSql &= "  AND equip_type_id = ?"
        szSql &= "  ORDER BY equip_id "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, g_factory_id, "LH", "LHJ")
        If IsNothing(dt) Then
            selectALL = New DataTable
        Else
            selectALL = dt
        End If
    End Function
    ''' <summary>
    ''' 设置硫化机的状态
    ''' </summary>
    ''' <param name="_EquipID">硫化机ID</param>
    ''' <param name="_status">状态：0：闲、1：忙、2：失联、3：温度or压力Error、4：待出炉</param>
    ''' <returns></returns>
    Public Function setLHJStatus(_EquipID As String, _status As String) As Integer
        Dim szSql As String = ""
        Dim rtn As Integer

        szSql &= " UPDATE sf.t_hw_setting "
        szSql &= "SET status = ? ,update_time=(select now())"
        szSql &= "WHERE   factory_id = ? "
        szSql &= "   and workshop_id = ?"
        szSql &= " and equip_type_id = ?"
        szSql &= " and equip_id = ?  "

        rtn = DBacs.ExecuteUpdateSql(szSql, -1, _status, g_factory_id, g_ws_lh, g_equiptype_LHJ, _EquipID)
        Return rtn
    End Function
End Class
